import odoo from '@/odoo'

const model = 'account.move'
const namespace = 'accountMoveForm'

const fields_default = {
  line_ids: {
    account_id: {},
    sub_account_id: {},
  }
}

const dvamodel = odoo.dvamodel({
  model, fields: fields_default
})

const {state, effects, reducers} = dvamodel

const Model = odoo.env(model);


export default {
  namespace,
  state:{
    ...state,

    displayRecord: {},
    formRecord: {},

    index: -1,

  },

  effects: {
    ...effects,
  },

  reducers: {
    ...reducers,

    view2(state, { payload }) {
      const {id, fields=fields_default, fix} = payload;

      if(fix!==undefined){
        return {
          ...state,
        }
      }


      if(id===0){
        return {
          ...state,
          index: -1,

          id: 0 ,
          displayRecord: {},
          formRecord: {},

        };

      }


      const rec = Model.view(id);

      const record = rec.look(fields);
      const { line_ids=[] } = record;

      const new_lines = line_ids.map(item=>{
        return { ...item }
      })

      const displayRecord = {...record, line_ids: new_lines};
      const formRecord = { line_ids: {} };

      return {
        ...state,
        id,
        record,
        index: -1,
        displayRecord,
        formRecord,

      };
    },


    formUpdate(state, { payload }) {

      const {journal_id} = payload;

      const vals = {
        ...payload,
      }

      if (journal_id){
        vals.journal_id = odoo.env['account.journal'].view(journal_id).look();
      }

      const { displayRecord, formRecord  } = state ;
      return {
        ...state,
        displayRecord:{
          ...displayRecord,
          ...vals,
        },
        formRecord:{
          ...formRecord,
          ...payload,
        },
      };
    },

    formDeleteLine(state, { payload }) {
      const {id } = payload;

      const {  formRecord,displayRecord } = state ;

      const newFormRecord = formDeleteLine_formRecord({ id,formRecord });
      const newDisplayRecord = formDeleteLine_displayRecord({ id, displayRecord });

      return {
        ...state,
        formRecord: newFormRecord,
        displayRecord: newDisplayRecord,

      };

    },


    formUpdateLine(state, { payload }) {
      const {id, vals } = payload;

      const { index, formRecord,displayRecord } = state ;
      const nid = id || index;

      const newIndex = id ? index : index - 1;

      const newFormRecord = formUpdateLine_formRecord({ id:nid, vals,formRecord });
      const newDisplayRecord = formUpdateLine_displayRecord({ id:nid, vals,displayRecord });

      console.log(newFormRecord, newDisplayRecord)

      return {
        ...state,
        index: newIndex,
        formRecord: newFormRecord,
        displayRecord: newDisplayRecord,

      };

    },

  },
};

const formUpdateLine_displayRecord = ({ id,vals, displayRecord  }) => {
      const { line_ids=[] } = displayRecord ;

      const {account_id, partner_id, product_id, analytic_account_id} = vals;

      const new_vals = {
        ...vals,
        account_id: account_id && odoo.env['account.account'].view(account_id).look(),
        partner_id: partner_id && odoo.env['res.partner'].view(partner_id).look(),
        product_id: product_id && odoo.env['product.product'].view(product_id).look(),
        analytic_account_id: analytic_account_id && odoo.env['account.analytic.account'].view(analytic_account_id).look(),
      };

      //console.log(vals, new_vals)


      const line_dict = line_ids.reduce((acc,cur)=>{
        acc[cur.id] = cur;
        return acc
      },{})

      line_dict[id] = { ...(line_dict[id] || {}), ...new_vals, id}

      return {
          ...displayRecord,
          line_ids: Object.values(line_dict),
      }

}


const formUpdateLine_formRecord = ({ id,vals, formRecord  }) => {
      const { line_ids={} } = formRecord ;

      const new_lines = {};
      new_lines[id] = [ id>0 ? 1 : 0, id,vals];

      return {
          ...formRecord,
          line_ids:{
            ...line_ids,
            ...new_lines,
          }
      }

}


const formDeleteLine_displayRecord = ({ id, displayRecord  }) => {
      const { line_ids=[] } = displayRecord ;

      const line_dict = line_ids.reduce((acc,cur)=>{
        acc[cur.id] = cur;
        return acc
      },{})

      delete line_dict[id];

      return {
          ...displayRecord,
          line_ids: Object.values(line_dict),
      }

}


const formDeleteLine_formRecord = ({ id, formRecord  }) => {
      const { line_ids={} } = formRecord ;
      delete line_ids[id]
      const new_lines = {};
      if ( id>0 ){
        new_lines[id] = [ 2, id, 0];
      }

      return {
          ...formRecord,
          line_ids:{
            ...line_ids,
            ...new_lines,
          }
      }
}
